angular.module('starter.controllers', [])

.controller('DashCtrl', function($scope,$ionicActionSheet, $ionicPopup) {
  $scope.login = function(){
    onDeviceReady();
  }
    function onDeviceReady() {
      console.log(Wechat);
      Wechat.isInstalled(function (installed) {
          alert("Wechat installed: " + (installed ? "Yes" : "No"));
      }, function (reason) {
          alert("Failed: " + reason);
      });
      var scope = "snsapi_userinfo",
          state = "_" + (+new Date());
      Wechat.auth(scope, state, function (response) {
          // you may use response.code to get the access token.
          alert(JSON.stringify(response));
      }, function (reason) {
          alert("Failed: " + reason);
      });
      // Add similar listeners for other events
    }////只是个小DEMO所以就直接登录微信了，如果需要单独登录稍微改一下就可以了
  
    $scope.share = function(title, desc, url, thumb) {
      console.log(12);
      $ionicActionSheet.show({
        buttons: [
          { text:'分享给微信好友' },
          { text:'分享至微信朋友圈' }
        ],
        titleText: '分享',
        cancelText:'取消',
        cancel: function() {
        // 取消时执行                
        },
        buttonClicked: function(index) {
          console.log(index);
          if(index == 0) {
            $scope.shareViaWechat(0, title, desc, url, thumb);
          }else if(index == 1) {
            $scope.shareViaWechat(1, title, desc, url, thumb);
          }
        }
      }); 
    };
    $scope.shareViaWechat = function(scene, title, desc, url, thumb) {
      //scene:1 表示分享到朋友圈
      //scene:0 表示分享给好友
      alert("scene:" + scene);
      var msg = {
        title: "Test sharing function",
        description: "This is a test",
        thumb: "https://mmbiz.qlogo.cn/mmbiz_png/04IIkW962ep720RYSbwx2hfHY81DeG92gRFAJBibRcEhe8RZZyoT6ddSNKWZwmHKfsH4hFyXichuGfKF5ry9dcYA/0?wx_fmt=png",
        mediaTagName: "TEST-TAG-001",
        messageExt: "Test fields for third party tape",
        messageAction: "<action>dotalist</action>",
        //media: "YOUR_MEDIA_OBJECT_HERE",
        //网页链接
        media: {
            type: Wechat.Type.WEBPAGE,
            webpageUrl: "https://open.weixin.qq.com"
        }
      }
      //分享文字
        // Wechat.share({
        //     text: "This is just a plain string",
        //     scene: Wechat.Scene.TIMELINE   // share to Timeline
        // }, function () {
        //     alert("Success");
        // }, function (reason) {
        //     alert("Failed: " + reason);
        // });
    //分享链接，设置好标题，描述，图片，链接 参数就OK。
      Wechat.share({
          message: msg,
          scene: scene   // share to Timeline
      }, function () {
          alert("Success");
      }, function (reason) {
          alert("Failed: " + reason);
      });
    };


    //QQ
    $scope.QQ_login = function(){
      console.log("QQ_login");
      var args = {};
      args.client = QQSDK.ClientType.QQ;//QQSDK.ClientType.QQ,QQSDK.ClientType.TIM;
      // 检测qq客户端是否安装
      QQSDK.checkClientInstalled(function () {
        alert('client is installed');
      }, function () {
        // if installed QQ Client version is not supported sso,also will get this error
        alert('client is not installed');
      }, args);
 
      QQSDK.ssoLogin(function (result) {
        alert('token is ' + result.access_token);
        alert('userid is ' + result.userid);
        alert('expires_time is ' + new Date(parseInt(result.expires_time)) + ' TimeStamp is ' + result.expires_time);
      }, function (failReason) {
        alert(failReason);
      }, args);
    }
    $scope.QQ_share = function(){
      console.log("QQ_share");
      $ionicActionSheet.show({
        buttons: [
          { text:'分享给QQ好友' },
          { text:'分享至QQ空间' }
        ],
        titleText: '分享',
        cancelText:'取消',
        cancel: function() {
        // 取消时执行                
        },
        buttonClicked: function(index) {
          console.log(index);
          QQ_plugin.shareNews(index, {
            title: '这个是 Cordova QQ 图片分享的标题',
            url: 'https://connect.qq.com',
            description: '这个是 Cordova QQ 图片分享的描述',
            image: 'https://mmbiz.qlogo.cn/mmbiz_png/04IIkW962ep720RYSbwx2hfHY81DeG92gRFAJBibRcEhe8RZZyoT6ddSNKWZwmHKfsH4hFyXichuGfKF5ry9dcYA/0?wx_fmt=png'
          })
        }
      }); 
    }
    var QQ_plugin = {
      isInstalled: function (suc, fail) {
        if (suc && fail && typeof suc === 'function' && typeof fail === 'function') {
            QQSDK.checkClientInstalled(suc, fail);
        }
      },
      // 登录
      login: function (suc, fail) {
        var args = {};
        args.client = QQSDK.ClientType.QQ;
        if (suc && fail && typeof suc === 'function' && typeof fail === 'function') {
            QQSDK.ssoLogin(function (res) {
                suc(res);
            }, function (failReason) {
                fail(failReason);
            }, args);
        }
    },

    // 退出登录
    logout: function (suc, fail) {
        if (suc && fail && typeof suc === 'function' && typeof fail === 'function') {
            QQSDK.logout(suc, function (failReason) {
                fail(failReason);
            });
        }
    },
    // 分享文字
    shareText: function (text, suc, fail) {
        var args = {};
        args.client = QQSDK.ClientType.QQ;
        args.scene = QQSDK.Scene.QQ;
        args.text = text;
        if (suc && fail && typeof suc === 'function' && typeof fail === 'function') {
            QQSDK.shareText(suc, function (failReason) {
                fail(failReason);
            }, args);
        }
    },

    // 分享图片
    shareImage: function (params, suc, fail) {
        var args = {};
        args.client = QQSDK.ClientType.QQ;
        args.scene = QQSDK.Scene.QQ;
        args.title = params.title;
        args.description = params.description;
        args.image = params.image;
        // if (suc && fail && typeof suc === 'function' && typeof fail === 'function') {
        //     QQSDK.shareImage(suc, function (failReason) {
        //         fail(failReason);
        //     }, args);
        // }
        QQSDK.shareImage(function () {
          alert('shareImage success');
        }, function (failReason) {
          alert(failReason);
        }, args);
    },

    // 分享新闻
    shareNews: function (num,params, suc, fail) {
        var args = {};
        args.client = QQSDK.ClientType.QQ;
        if(num == 0){
          args.scene = QQSDK.Scene.QQ;
        }else if(num == 1){
          args.scene = QQSDK.Scene.QQZone;
        }
        
        args.url = params.url;
        args.title = params.title;
        args.description = params.description;
        args.image = params.image;
        // if (suc && fail && typeof suc === 'function' && typeof fail === 'function') {
        //     QQSDK.shareNews(suc, function (failReason) {
        //         fail(failReason);
        //     }, args);
        // }
        QQSDK.shareNews(function () {
          alert('shareImage success');
        }, function (failReason) {
          alert(failReason);
        }, args);
    },

    // 分享音乐
    shareAudio: function (params, suc, fail) {
        var args = {};
        args.client = QQSDK.ClientType.QQ;
        args.scene = QQSDK.Scene.QQ;
        args.url = params.url;
        args.title = params.title;
        args.description = params.description;
        args.image = params.image;
        args.flashUrl = params.flashUrl;
        if (suc && fail && typeof suc === 'function' && typeof fail === 'function') {
            QQSDK.shareAudio(suc, function (failReason) {
                fail(failReason);
            }, args);
        }
    }
  }
  $scope.weibo_login = function(){
    console.log("weibo_login");
    WeiboSDK.checkClientInstalled(function () {
      alert('client is installed');
   }, function () {
      alert('client is not installed');
   });
    WeiboSDK.ssoLogin(function (args) {
      alert('access token is ' + args.access_token);
      alert('userId is ' + args.userId);
      alert('expires_time is ' + new Date(parseInt(args.expires_time)) + ' TimeStamp is ' + args.expires_time);
   }, function (failReason) {
      alert(failReason);
   });
  }
  $scope.weibo_share = function(){
    console.log("weibo_share");
    var args = {};
    args.url = 'https://cordova.apache.org/';
    args.title = 'Apache Cordova';
    args.description = 'This is a Cordova Plugin';
    args.image = 'https://cordova.apache.org/static/img/pluggy.png';
    WeiboSDK.shareToWeibo(function () {
       alert('share success');
    }, function (failReason) {
       alert(failReason);
    }, args);
  }
})

.controller('ChatsCtrl', function($scope, Chats) {
  // With the new view caching in Ionic, Controllers are only called
  // when they are recreated or on app start, instead of every page change.
  // To listen for when this page is active (for example, to refresh data),
  // listen for the $ionicView.enter event:
  //
  //$scope.$on('$ionicView.enter', function(e) {
  //});

  $scope.chats = Chats.all();
  $scope.remove = function(chat) {
    Chats.remove(chat);
  };
})

.controller('ChatDetailCtrl', function($scope, $stateParams, Chats) {
  $scope.chat = Chats.get($stateParams.chatId);
})

.controller('AccountCtrl', function($scope) {
  $scope.settings = {
    enableFriends: true
  };
});
